/***************************************************************************************
Firm-embedded productivity and cross-country income differences
Alviarez, Cravino and Ramondo
Journal of Political Economy (2022)

Program: model_01_baseline.do
Date: October 2022

Description: Constructing the DA dummies (aggregate country effects) under the assumption that technology transfer costs have an origin-specific component

*****************************************************************************************/

*-------------------------------------------------------------------------------
global typeden=1
include "set_directories.do"
set memory 64g
global lf "LF"

*Log
cap log close
log using "${clogs}/model_08_byyear.log", replace
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
*Baseline (for each individual year)
*-------------------------------------------------------------------------------
global conditionforage ""
include "${cmain}/prep_01_forreg.do"

local heading "baseline"  
local varinterest sales
local timeframe `" "2006" "2007" "2008" "2009" "2010" "2011" "2012" "2013" "2014" "2015" "2016" "2017" "' 



if "`heading'"=="baseline" {
*-----------------------------------------------------------
local ending "" 
local zz naics  
local var0 "_`varinterest'" 
local name "_base" 
local name1 "_woparent" 
local samp=1  
local title "`zz'`var0'_s`samp'`name'`name1'`ending'"
display "`title'"

global ctryrel FR
local period "`timeframe'"
global nonmarket `" drop if sector1=="" "' 
global cont_firms `" keep if years_after2010>=0 "'
global cont_firms2 `" keep if years_allperiod>=0 "'
local absFEreg "guoFE"
global ind_var dist_sec* lang_sec* 
global guoFEonly=0 
global LHS "ln(sin`var0'`denominator')" 
global numctry `" keep if num_ctry>=0 "' 
global pctiles  `" keep if sin_sales>=0 "'  
global other_restr  `" keep if isocode!=hq "' 
global other_restr2  ""

}
*
display "`title'"
local affparent "0" /*"0" is the default, "1" if keeping even one affiliate after parents are dropped*/ 
include "${cmain}/reg_01_naics.do"

*Aggregating the sectoral estimates (Aggregate)
local weightsused_FR=0
local typeused "agg" 
include "${cmain}/reg_02_agg.do"

*Aggregating the sectoral estimates (Aggregate, using FR sectoral shares as weights)
local weightsused_FR=1
local typeused "agg" 
include "${cmain}/reg_02_agg.do"

*Aggregating the sectoral estimates (Manufacturing, Services, and Others)
local typeused "type"
include "${cmain}/reg_02_agg.do"





*-------------------------------------------------------------------------------
*Excluding MNEs that do not appear in ORBIS every year between 2010-2016 (for each individual year)
*-------------------------------------------------------------------------------
global cond_ctte `" dummy_ctte==1 "'
include "${cmain}/prep_02_forreg_cttesample.do"

local heading "base_woparent_ctte"  
local timeframe `" "2007" "2008" "2009" "2010" "2011" "2012" "2013" "2014" "2015" "2016" "2017" "' 
local varinterest sales 


if "`heading'"=="base_woparent_ctte" {
*-----------------------------------------------------------
local ending "_ctte" 
local zz naics  
local var0 "_`varinterest'" 
local name "_base" 
local name1 "_woparent" 
local samp=1  
local title "`zz'`var0'_s`samp'`name'`name1'`ending'"
display "`title'"

global ctryrel FR
local period "`timeframe'"
global nonmarket `" drop if sector1=="" "' 
global cont_firms `" keep if years_after2010>=0 "'
global cont_firms2 `" keep if years_allperiod>=0 "'
local absFEreg "guoFE"
global ind_var dist_sec* lang_sec* 
global guoFEonly=0 
global LHS "ln(sin`var0'`denominator')" 
global numctry `" keep if num_ctry>=0 "' 
global pctiles  `" keep if sin_sales>=0 "'  
global other_restr  `" keep if isocode!=hq "' 
global other_restr2  ""
}
*
display "`title'"
local affparent "0" /*"0" is the default, "1" if keeping even one affiliate after parents are dropped*/ 
include "${cmain}/reg_01_naics.do"

*Aggregating the sectoral estimates (Aggregate)
local weightsused_FR=0
local typeused "agg" 
include "${cmain}/reg_02_agg.do"


*Erase intermediate files 
*---------------------------------------------------------
erase "${data}/ctte_naics_sales_s1_base_woparent_ctte.dta"
erase "${data}/firmFE_naics_sales_s1_base_woparent_ctte.dta"
erase "${data}/residual_naics_sales_s1_base_woparent_ctte.dta"
erase "${data}/gravity_naics_sales_s1_base_woparent_ctte.dta"
erase "${data}/gravitycoef_naics_sales_s1_base_woparent_ctte.dta"
erase "${data}/reg0_naics_sales_s1_base_woparent_ctte_2016.dta"
erase "${data}/pre_reg0_naics_sales_s1_base_woparent_ctte_2016.dta"





log close


